In this video, we're going to learn about functions.
And a function is a block of code that you can call.
Functions are great for handling repetitive tasks and also for handling events.
Like I add this button right here.
If I press it, it's going to fire an event that calls a function to create steps, and each step uses
a function to create the step itself.
I thought that would be a pretty cool way to learn about functions.
Let's go ahead and get started with that.
All right.
So I think I'm going to use a base plate today, click that just to change things up.
And the big difference with the baseplate, especially for us today, is the Y axis.
The height of the baseplate starts at zero on the terrain, the Y starts at ten.
It used to be 2.5, and then they increase the thickness of the terrain.
This is going to be zero.
Just keep that in mind.
All right.
So let's go ahead and get started with our steps.
Let's go to this workspace, open the workspace up, hit that plus sign, add a script, or we can just
type out as R to see the list of things that begin with SCR.
There we go.
We've got a script.
Let's call the script steps.
All right.
Now, before we had a part in the workspace in the patio video and then we just modified it.
But here what I'm going to do is I'm going to create a part from scratch.
So I'll do my local part variable.
But instead of equals script parent, we're going to have equals instance dot new.
So we are creating a new instance of parentheses, speech, quote, part.
We can create a lot of cool stuff and we will.
But right now we're focusing on parts.
The second argument is going to be the parent of the part.
You have to have it in the workspace or a descendant of something in the workspace in order for it to
show up in your game.
So we'll say workspace.
Cool.
We also need to set the position, the default position of a part when you create it is going to be
000.
Well, that's where our base plate is.
And then terrain, it's actually ten studs above that.
So you won't even see it in the terrain.
Let's go ahead and create the part or create the position.
Create the position.
Vector should say we'll do new zero on the x that center of the world on the x axis to studs.
Hi.
We can do that because the base plate starts at zero and then we'll move it.
Three studs on the Z axis.
Cool.
Let's change the size of it.
That's also a vector three, right?
So the default size, if you create a part, will be four one and two.
Let's make this look like a step.
So we're going to say ten studs on the X is going to be kind of a long step, 0.5 studs high, not too
thick, and then we'll make this three studs deep.
Cool.
But because the position is a little off the ground, we don't want it to fall.
Let's anchor it.
So we'll say part anchored equals true.
All right.
Let's play it.
See what we got.
There we go.
It's floating above our spa location.
Notice these little studs right here.
That's where the term stud comes from.
The distance between these two things.
It's from the Lego days.
And if you look on the bottom, I don't know if you could see it.
You could see.
Yeah, there we go.
You could see little studs underneath.
Those are the little inlets of the studs.
Right.
All right.
So let's get rid of that.
Let's make that smooth pit stop.
And we'll do part.
Now we have something called surface type that begins with an S surface.
Top surface.
I'm sorry.
Top surface, which is a surface type.
There we go.
Top surface enum.
We learned about that for.
And we got to get a surface type here.
There we go.
Surface type.
Smooth.
Cool.
So this enum just holds a whole bunch of stuff that you might need in road roadblocks.
And one of those things is surface type.
Surface type has a whole bunch of different surface types and we've got smooth.
We're going to do that for the bottom two bottom surface equals enum air, enum surface type smooth.
Now we've got a smooth step.
Let's try it.
All right, there's our step now.
We want to make a couple of steps.
How do we do that?
Well, let's pretend we don't know about functions.
Most of you guys already do.
Let's copy this code control C and we'll make three steps.
Control V, control V, we've got three steps here.
Right?
But they're going to be exactly in the same position.
We have to change the position.
So this is two and three.
Let's make this position here for the second step.
Come up by four and then move it down the Z another three.
So we'll make that six.
We're going to do the same thing on this step.
It was two then for now.
We'll make that six.
We'll make this nine.
There we go.
Let's give it a shot.
There.
We got three steps.
Let's take a look at this code, because if you are a Java programmer, you'd be like, Hey, we just
re declared this several times.
How come we don't have an error?
Well, I'll let you do that.
But if you feel better, what we can do is get rid of that local.
Makes it look like a declaration.
And now we have the part.
We're just re assigning another value.
So it is three parts, but we only have a reference to the latest one.
Right.
So this part right here is going to be pointing to this instance of the part.
So all the changes that we make here are right are this part right here.
So if I do something like part brick, color equals brick color dot red parentheses, only the last
one will be red.
It's not going to be all of these.
Right, because we get a new address every time we're pointing to a new reference of the part.
There you go.
See, just the last one.
All right.
Now, this is going to get really, really repetitive, right?
Especially if we have to change something like the size.
You're going to have to go through every single line.
But everything is so close to the same thing except possession.
While we could do a function and then just pass in the position that we want.
So I'm going to say a local function make step.
Right.
We'll get this code right here.
Control X to cut.
Control V to paste.
I get rid of these two.
Let's keep our brick color, because I want to look at this real quick.
See how that complained unknown global part.
It doesn't know what this thing is because we declared it inside this code block.
We talked about scope before, but it was up here on the top.
We had the local part.
Well, that's local to the script, just like this is local to the script.
Once we put it inside a function that's a smaller code block, we can't see it outside this function.
It's local to that function.
All right.
Well, that's okay.
I will show you how to get around stuff like that for now.
How about we change the color by doing this?
That way, we'll change every step color and we'll make it random.
Cool beans.
Now, will this run?
No.
We have to call, make step.
We have to call it somehow.
Well, it's easy.
Let's copy that.
Boom.
And then we paste it.
Now, what's going to happen is the interpreter is going to start this script up in the game or start
reading the script.
When the game starts up.
It's going to read this in the memory, it's going to know it exists.
And then when we call it, it's going to fire it.
If this was not here, this would not run.
The interpreter would know about make step, but it wouldn't be running it now.
It does.
We only have one step though.
Bummer.
We need to make many steps.
Well, this is the only thing that needs to change.
Wouldn't it be great?
Notice this vector three new as an X, Y and z.
We need to change the Y and the Z.
Let's just call this a Y and a Z.
These are going to be variables.
It's complaining now that you see that red line unknown global z.
Well, we can declare those variables up here.
These are called parameters in the function definition.
They're going to be local variables to this function.
If we go down here and we pass the numbers two and three, this two is going to be assigned to that
Y.
And then when we use that Y, it's going to be a two.
Same with three for Z.
Oh, that's a three now and then that's a three.
Why is that?
Great.
Well, we can do this copy.
Paste, paste, paste.
Let's make four steps.
So these are incrementing by two.
So we'll say two, four, six, eight.
These are increasing by three.
So it's three, six, nine, 12.
Let's see what we got.
Oh, look at that.
The random numbers aren't that good.
But don't worry, when we do tables, we're going to change that stuff.
All right.
One more thing.
We haven't done any return statements and stuff like that.
That'll be the next video.
I'm going to move that up, get rid of that whitespace.
I'm just going to put a little pause in here.
In between each step, I'm going to wait 0.2 seconds.
And then before I start making any steps, I want to see them be made.
So I'm going to say, wait, I'll do 7 seconds, let's do 6 seconds, and then watch them be made.
Hey, we go.
And any second.
There they go, the four steps and we have different colors.
All right.
Next video we're going to do return statement and we're going to do some other stuff.